/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.server.service.impl;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.daikit.commons.shared.utils.DkIdGenerator;
import com.daikit.daikit4gxt.shared.bean.DkConnectionData;
import com.daikit.daikit4gxt.shared.bean.DkConnectionReturnData;
import com.daikit.daikit4gxtsample.server.scope.SessionParametersContainer;
import com.daikit.daikit4gxtsample.server.service.SessionService;
import com.daikit.daikit4gxtsample.shared.bean.User;


@SuppressWarnings("javadoc")
@Service
public class SessionServiceImpl implements SessionService
{

	protected final Log log = LogFactory.getLog(getClass());

	@Autowired
	private Environment env;
	@Autowired
	protected SessionParametersContainer sessionParametersContainer;

	private final User user1 = new User();

	/**
	 * Initialization method
	 */
	@PostConstruct
	public void initialize()
	{
		user1.setId(DkIdGenerator.generateRandomId(6));
		user1.setEmail(env.getProperty("user1.email"));
		user1.setFirstName(env.getProperty("user1.firstname"));
		user1.setLastName(env.getProperty("user1.lastname"));
		user1.setPassword(env.getProperty("user1.password"));
	}

	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
	// HTTP SESSION
	// *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

	@Override
	public void disconnect(final HttpServletRequest request, final HttpServletResponse response)
	{
		log.debug("[ENTER] disconnect(...)");
		sessionParametersContainer.clear();
		log.debug("[LEAVE] disconnect(...)");
	}

	@Override
	public User getHttpSessionUser()
	{
		return sessionParametersContainer.getLoggedUser();
	}

	@Override
	public String getHttpSessionUserId()
	{
		final User httpSessionUser = sessionParametersContainer.getLoggedUser();
		return httpSessionUser == null ? null : httpSessionUser.getId();
	}

	@Override
	public DkConnectionReturnData connectWithoutSecurity(final DkConnectionData connectionData)
	{
		final String user1email = user1.getEmail();
		final String user1password = user1.getPassword();
		User ret = null;
		if (user1email.equals(connectionData.getLogin()) && user1password.equals(connectionData.getPassword()))
		{
			sessionParametersContainer.update(user1, DkIdGenerator.generateRandomId(16));
			ret = user1;
		}
		return new DkConnectionReturnData(ret);
	}

	@Override
	public void testConnectionTimeout()
	{
		sessionParametersContainer.clear();
	}
}
